package CH01数组结构.LeetCode栈与队列;

import java.util.LinkedList;

public class LC225 {
    class MyStack {
        LinkedList<Integer> queueA;
        LinkedList<Integer> queueB;
        public MyStack() {
            queueA = new LinkedList<>();
            queueB = new LinkedList<>();
        }

        public void push(int x) {
            if (queueA.isEmpty() && queueB.isEmpty()) {
                queueA.offer(x);
            } else if (queueA.isEmpty()) {
                queueB.offer(x);
            } else {
                queueA.offer(x);
            }
        }

        public int pop() {
            if (queueA.isEmpty()) {
                while (queueB.size() != 1) {
                    queueA.offer(queueB.poll());
                }
                return queueB.poll();
            } else {
                while (queueA.size() != 1) {
                    queueB.offer(queueA.poll());
                }
                return queueA.poll();
            }
        }

        public int top() {
            if (queueA.isEmpty()) {
                return queueB.getLast();
            } else {
                return queueA.getLast();
            }
        }

        public boolean empty() {
            return queueA.isEmpty() && queueB.isEmpty();
        }
    }
}